//
//  MindPageView.m
//  MindBox
//
//  Created by MS on 16-1-13.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//
#define POPVIEW_HEIGHT 250
#define MENUVIEW_HEIGHT 35
#define MENUVIEW_Y  CGRectGetMaxY(self.bottomView.frame)

#import "MindPageView.h"
#import "MenuView.h"
#import "PopView.h"
#import "LGCover.h"
#import "MindHeaderView.h"
#import "UIImageView+WebCache.h"
@interface MindPageView ()<MenuViewDelegate,PopViewDelegate,LGCoverDelegate>
@property (nonatomic,weak) MenuView * menuView;
@property (nonatomic,weak) MindHeaderView * mindHeaderView;
@property (nonatomic,weak) PopView * popView;
@property (nonatomic,weak) LGCover * coverView;
@property (nonatomic,strong) Param * param;

@end

@implementation MindPageView

-(void)setMind_DetailData:(Mind_Detail *)mind_DetailData
{
    _mind_DetailData = mind_DetailData;
    
    self.bottomView.title.text = [NSString stringWithFormat:@"共%@件礼物",mind_DetailData.goods_count];
 
    [self.collectionView reloadData];
}

-(void)setMind_Banner:(Mind_Banner *)mind_Banner
{
    _mind_Banner = mind_Banner;

    self.mindHeaderView.banner = mind_Banner;
    
    [self.collectionView reloadData];
}


-(MindHeaderView *)mindHeaderView
{
    if (_mindHeaderView==nil) {
        MindHeaderView * view = [MindHeaderView mindheadViewWithXib];
        _mindHeaderView = view;
        
        __weak typeof(self) weakSelf = self;
        [_mindHeaderView setMindHeaderBlock:^(Mind_Banner *banner) {
            if (weakSelf.MindPageViewBlock) {
                weakSelf.MindPageViewBlock();
            }
        }];
       
        [self.collectionView addSubview:view];
    }
    return _mindHeaderView;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self createPopView];
        [self.collectionView bringSubviewToFront:self.bottomView];
        MenuView * view = [[MenuView alloc] init];
        self.menuView = view;
        _menuView.delegate = self;
        _menuView.titleArray = @[@"综合",@"价格",@"热度",@"最新",@"筛选"];
        [self.collectionView addSubview:view];
    }
    return self;
}

-(void)createPopView
{
    PopView * popView = [PopView popViewWithXib];
    self.popView = popView;
    popView.delegate = self;
    popView.frame = CGRectMake(0, MENUVIEW_Y+MENUVIEW_HEIGHT-POPVIEW_HEIGHT, self.width, POPVIEW_HEIGHT);
    
    [self.collectionView addSubview:popView];
    
}


-(void)menuViewDidClick:(MenuView *)menuView index:(NSInteger)index type:(BOOL)sortType
{
 
    /**
     *  选择条悬停时点击回到头部位置
     */
    if (index!=4 || (index==4 && self.collectionView.contentOffset.y<=MENUVIEW_Y)) {
        [self.collectionView setContentOffset:CGPointMake(0, CGRectGetMaxY(self.bottomView.frame))animated:YES];
    }
    
    
    /**
     *  PopView,蒙板动画设置
     */
    
    if (index==4 && menuView.speBtn.selected==NO) {
        
        LGCover * coverView = [LGCover show];
        coverView.frame = CGRectMake(0, _menuView.y, self.width,self.height);
        coverView.backgrondAlph = YES;
        self.coverView = coverView;
        coverView.delegate = self;
        [self.collectionView insertSubview:coverView belowSubview:_popView];
        _popView.hidden = NO;
        [UIView animateWithDuration:0.25 animations:^{
            _popView.y = _menuView.y+MENUVIEW_HEIGHT;
            self.collectionView.scrollEnabled = NO;
        }];
    }
    
    else {
        
        [UIView animateWithDuration:0.25 animations:^{
            _popView.y = _menuView.y+MENUVIEW_HEIGHT-POPVIEW_HEIGHT;
            [_coverView removeFromSuperview];
        } completion:^(BOOL finished) {
            self.collectionView.scrollEnabled = YES;

            _popView.hidden = YES;
            [_popView resetValue];
        }];
        
    }
    
    
    /**
     *  参数模型设置
     */
    
    if (index!=4) {
        Param * pa = [Param param];
        self.param = pa;
        if (index==2 || index==3) {//搜索类型对应的filter_type，热度：3，最新：4
            pa.filter_type = index+1;
        }
        else if (index==1 ){//价格升序：1，降序：2
            pa.filter_type = sortType+1;  //sortType 0升序，1降序
        }
        else if(index==0){//综合
            pa.filter_type = 0;
        }
        
        if ([self.delegate_min respondsToSelector:@selector(mindPageMenuViewDidClick:param:index:)]) {
            [self.delegate_min mindPageMenuViewDidClick:self param:pa index:index];
        }
        
    }


}

-(void)popViewFnishBtnDidClick:(PopView *)popview
{
    self.param.free_postage = popview.freePocks;
    self.param.is_stock = popview.hasGoods;
    self.param.min_price =(int)popview.lowerValue;
    
    if (popview.upValue>1000) {
        self.param.max_price =-1;
    }
    else{
        self.param.max_price =(int)popview.upValue;
    }
    
    if ([self.delegate_min respondsToSelector:@selector(mindPageMenuViewDidClick:param:index:)]) {
        [self.delegate_min mindPageMenuViewDidClick:self param:self.param index:self.menuView.selBtn.tag];
    }
    
    [self hiddenPopView];
    [_coverView removeFromSuperview];
}


-(void)coverDidClick:(LGCover *)cover
{
    [self hiddenPopView];
    
}


-(void)hiddenPopView
{
    [UIView animateWithDuration:0.25 animations:^{
        _popView.y = _menuView.y+MENUVIEW_HEIGHT-POPVIEW_HEIGHT;
        
    } completion:^(BOOL finished) {
        _popView.hidden = YES;
         self.collectionView.scrollEnabled = YES;
        [_popView resetValue];
        _menuView.speBtn.selected = NO;
    }];
}


-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
   return UIEdgeInsetsMake(CGRectGetMaxY(self.bottomView.frame)+_menuView.height+10, 10, 0, 10);
    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    /**
     *  设置选择条悬停
     */
    if (scrollView.contentOffset.y>= CGRectGetMaxY(self.bottomView.frame)) {
        
        self.menuView.y =scrollView.contentOffset.y;
    }
    
    else{
        self.menuView.y = CGRectGetMaxY(self.bottomView.frame);
    }
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.mindHeaderView.frame = CGRectMake(0, 0, self.width, 200);
    self.bottomView.frame = CGRectMake(0, CGRectGetMaxY(_mindHeaderView.frame), self.width, 30);
    
    self.menuView.frame = CGRectMake(0, CGRectGetMaxY(self.bottomView.frame), self.width, MENUVIEW_HEIGHT);


}



@end
